<?php
/**
 * Letter model test file.
 *
 * Infectamic: browser game (http://infectamic.goldzoneweb.info)
 * Copyright 2011, Loic Chardonnet
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 */

App::import('Model', 'Letter');

/**
 * Class for Letter model test.
 */
class LetterTestCase extends CakeTestCase {
    var $fixtures = array('app.letter', 'app.letter_link');

    var $letters = array();

    function startTest() {
        $this->Letter =& ClassRegistry::init('Letter');
        $this->letters = $this->Letter->getLettersAndLinksInArray();
    }

    function endTest() {
        unset($this->Letter);
        ClassRegistry::flush();
    }

    function testPossibilityToGenerateNames() {
        $namesToGenerate = array(
            'alfie',
            'amelia',
            'charlie',
            'chloe',
            'emily',
            'evie',
            'george',
            'grace',
            'harry',
            'jack',
            'james',
            'jessica',
            'joshua',
            'lily',
            'oliver',
            'olivia',
            'ruby',
            'sophie',
            'thomas',
            'william',
        );

        foreach ($namesToGenerate as $nameToGenerate) {
            $this->assertTrue($this->canGenerateName($nameToGenerate));
        }
    }

    function testImpossibilityToGenerateNames() {
        $namesToGenerate = array(
            'sfrusphndste',
            'hm',
            'tharswvuscgyhrqqo',
            'fhcxczkcbvomyvducj',
            'vvdhdybgmbzldnzcykqxi',
            'gjnkn',
            'qrgouhzwvfhsafeq',
        );

        foreach ($namesToGenerate as $nameToGenerate) {
            $this->assertFalse($this->canGenerateName($nameToGenerate));
        }
    }

    /**
     * Checks if it is possible to generate the given name.
     *
     * @param string $name The name
     * @return boolean True on success, or else false
     */
    function canGenerateName($name) {
        $canGenerateName = true;

        $nameLenght = strlen($name);
        for ($letter = 1; $letter < $nameLenght; $letter++) {
            if (false === $this->areLettersLinked($name[$letter - 1], $name[$letter])) {
                $canGenerateName = false;
                break;
            }
        }

        return $canGenerateName;
    }

    /**
     * Checks if the first given letter is linked to the second.
     *
     * @param string $letter The first letter
     * @param string $linkedLetter The second letter
     * @return boolean True on success, or else false
     */
    function areLettersLinked($letter, $linkedLetter) {
        $areLettersLinked = false;
        if (true === array_key_exists($linkedLetter, $this->letters[$letter])) {
            $areLettersLinked = true;
        }

        return $areLettersLinked;
    }
}
